package com.xique.park.service.command.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.constant.HttpStatus;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.command.CommandResultUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkChargeRuleCommandApi;
import com.xique.park.bean.domain.ParkChargeCarTemp;
import com.xique.park.bean.domain.ParkChargeRuleComm;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.command.service.IParkChargeRuleCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 收费规则下发命令相关方法
 *
 * @author: xique
 */
@Service
public class ParkChargeRuleCommandServiceImpl extends CommandCommServiceImpl implements IParkChargeRuleCommandService {

    @Autowired
    private IParkChargeRuleCommandApi chargeRuleCommandApi;

    @Override
    public AjaxResult downChargeTempList(Long parkId, String sn, List<ParkChargeCarTemp> list) {
        JSONObject dataObj = new JSONObject();
        dataObj.put(CommandConstant.LIST, list);
        List<Long> recIdList = list.stream().map(ParkChargeCarTemp::getId).collect(Collectors.toList());
        dataObj.put(CommandConstant.REC_IDS, recIdList);

        JSONObject obj = getBaseObj(parkId, sn, ParkApiName.COMMAND_SET_MNGTYPE_CHARGE, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, dataObj);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = chargeRuleCommandApi.downMngTypeTempCharge(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult downChargeTempList(Long parkId, List<ParkChargeCarTemp> list) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (!CollectionUtils.isEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = downChargeTempList(parkId, devCode, list);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append(",");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success("下发成功");
                } else {
                    return AjaxResult.commandError("下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success("保存成功！车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError("车场参数不能为空");
        }
    }

    @Override
    public AjaxResult delChargeTempList(String sn, List<ParkChargeCarTemp> list) {
        JSONObject obj = getBaseObj(SecurityUtils.getParkId(), sn, ParkApiName.COMMAND_DEL_MNGTYPE_CHARGE, ParkApiName.VER_1_0);
        obj.put(CommandConstant.DATA, JSONArray.toJSON(list));

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = chargeRuleCommandApi.delMngTypeTempCharge(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult delChargeTempList(Long parkId, List<ParkChargeCarTemp> list) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (!CollectionUtils.isEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = delChargeTempList(devCode, list);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append("，");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success("下发成功");
                } else {
                    return AjaxResult.error("下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success("删除成功！车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.error("车场参数不能为空");
        }
    }

    @Override
    public AjaxResult downChargeRuleCommList(Long parkId, String sn, List<ParkChargeRuleComm> list) {
        JSONObject dataObj = new JSONObject();
        List<Long> recIdList = list.stream().map(ParkChargeRuleComm::getId).collect(Collectors.toList());
        dataObj.put(CommandConstant.REC_IDS, recIdList);
        JSONArray jsonArray = new JSONArray(list.size());
        for (ParkChargeRuleComm ruleComm : list) {
            if (null != ruleComm) {
                JSONObject obj1 = new JSONObject();
                obj1.put("rec_id", ruleComm.getId());
                obj1.put("template", ruleComm.getTemplate());
                obj1.put("price", StringUtils.isNotEmpty(ruleComm.getContent()) ? JSON.parseObject(ruleComm.getContent()) : null);
                jsonArray.add(obj1);
            }
        }
        dataObj.put(CommandConstant.LIST, jsonArray);

        JSONObject obj = getBaseObj(parkId, sn, ParkApiName.COMMAND_DOWN_CHARGE_TEMPLATE, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, dataObj);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = chargeRuleCommandApi.downChargeRuleCommon(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult downChargeRuleCommList(Long parkId, List<ParkChargeRuleComm> list, String reminderMsg) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (!CollectionUtils.isEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = downChargeRuleCommList(parkId, devCode, list);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append("，");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success(reminderMsg + "下发成功");
                } else {
                    return AjaxResult.commandError(reminderMsg + "下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success(reminderMsg + "车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError(reminderMsg + "下发失败，车场参数不能为空");
        }
    }

    @Override
    public AjaxResult delChargeRuleCommList(String sn, List<ParkChargeRuleComm> list) {
        JSONArray array = new JSONArray(list.size());
        for (ParkChargeRuleComm ruleComm : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("template", ruleComm.getTemplate());
            jsonObject.put("rec_id", ruleComm.getId());
            array.add(jsonObject);
        }

        JSONObject obj = getBaseObj(SecurityUtils.getParkId(), sn, ParkApiName.COMMAND_DEL_CHARGE_TEMPLATE, ParkApiName.VER_1_0);
        obj.put(CommandConstant.DATA, array);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = chargeRuleCommandApi.delChargeRuleCommon(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult delChargeRuleCommList(Long parkId, List<ParkChargeRuleComm> list) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (!CollectionUtils.isEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = delChargeRuleCommList(devCode, list);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append("，");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success("下发成功");
                } else {
                    return AjaxResult.commandError("下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success("删除成功！车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError("下发失败，车场参数不能为空");
        }
    }
}
